<?php

/*
class TextBinder, class for binding ini files for languages
Copyright (C) 2011  Petr Čermák

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Checker.php';

class TextBinder{

    private $filePaths = array();
    private $langArray = array();
    private static $lang;
    private static $instances = array();
    private static $defaultLang;

    private function __construct(){}

    public function addTextFile($lang, $path){
        Checker::checkFile($path);
        $this->filePaths[$lang] = $path;
    }

    public function getTextArray($lang = null){
        if($lang === null)$lang = self::$lang;
        Checker::checkString($lang);
        if(array_key_exists($lang, $this->langArray))
            return $this->langArray[$lang];
        else if(array_key_exists($lang, $this->filePaths)){
            $this->langArray[$lang] = parse_ini_file($this->filePaths[$lang]);
            return $this->langArray[$lang];
        }
        return null;
    }

    public static function setDefaultLanguage($lang){
        Checker::checkString($lang);
        self::$defaultLang = $lang;
        self::$lang = $lang;
    }

    public static function saveToSession(){
        $_SESSION['TextBinder_instances'] = self::$instances;
    }

    public static function loadFromSession(){
        self::$instances = $_SESSION['TextBinder_instances'];
    }

    public static function setLanguage($lang){
        if($lang === null)$lang = self::$defaultLang;
        Checker::checkString($lang);
        self::$lang = $lang;
    }

    public static function getSelectedLanguage(){
        return self::$lang;
    }

    public static function getInstance($name){
        Checker::checkString($name);
        if(!isset(self::$instances[$name]))
            self::$instances[$name] = new TextBinder();
        return self::$instances[$name];
    }

}
?>
